package com.taobao.phoenix.qtdemo.tools;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;


public class TaskGenerator {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		String adContentFile = "D:\\test\\ad1.txt";
		String targetUserFile = "D:\\test\\target1.txt";
		String workerUserFile = "D:\\test\\worker1.txt";
		String taskOutputFile = "D:\\test\\task1.txt";
		
		// step1, load ad content
		List<List<String>> adContents = new ArrayList<List<String>>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(adContentFile), "UTF-8"));
		String line = null;
		while((line=reader.readLine())!=null) {
			String tmp[] = line.split("\\|");
			List<String> tmpList = new ArrayList<String>();
			for(int i=0; i<tmp.length; i++) {
				tmpList.add(tmp[i]);
			}
			adContents.add(tmpList);
		}
		reader.close();
		System.out.println("load ad content success, size="+adContents.size());
		
		// step2, load worker usernick and pwd
		LinkedList<String> workers = new LinkedList<String>();
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(workerUserFile), "UTF-8"));
		line = null;
		while((line=reader.readLine())!=null) {
			char c = line.charAt(0);
			if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
				workers.add(line.trim());
			else
				workers.add(line.trim().substring(1));
		}
		reader.close();
		System.out.println("load wokers success, size="+workers.size());
		
		
		// step3, load target usernick
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(taskOutputFile), "UTF-8"));
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(targetUserFile), "UTF-8"));
		line = null;
		int id=1;
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		
		int maxUserSendNum = 30;
		int currentUserSendNum = 0;
		String[] workerInfo = null;
		while((line=reader.readLine())!=null) {
			sb.delete(0, sb.length());
			String nick = line.trim();
			sb.append(id++).append("\t").append(nick).append("\t");
			for(List<String> sentences : adContents) {
				int index = random.nextInt(sentences.size());
				sb.append(sentences.get(index)).append("|");
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append("\t");
			
			if(currentUserSendNum==0) {
				if(workers.isEmpty()) {
					System.out.println("no avaliable worker, stop");
					break;
				}
				String worker = workers.removeFirst();
				workerInfo = worker.split("\t");
			}
			sb.append(workerInfo[0]).append("\t").append(workerInfo[1]);
			
			currentUserSendNum++;
			if(currentUserSendNum==maxUserSendNum) {
				currentUserSendNum = 0;
			}
			writer.println(sb.toString());
		
		}
		reader.close();
		writer.close();
		System.out.println("generate tasks success!");
	}

}
